<?php

use app\model\Common;if ($system['wxapp'] != 0 && $system['softmode'] != 0) {
    require APP_PATH . "web/controller/Theme_wxapp.php";
}
if ($system['fanpai'] == 1 || $system['islottery'] == 1) {
    require APP_PATH . "web/controller/Theme_fanpai.php";
}
if ($system['islottery'] == 1) {
    require APP_PATH . "web/controller/theme/lt.php";
}
if ($system['ruanjian'] == '5') {
    require APP_PATH . "web/controller/Theme_bargain.php";
}
if ($system['ruanjian'] == 0 && $system['softstyle'] == 5) {
    require APP_PATH . "web/controller/Theme_jiameng.php";
}
if ($system['fenxiao'] == 1) {
    require APP_PATH . "web/controller/Theme_comm.php";
}
if ($system['iskoi'] == 1) {
    require APP_PATH . "web/controller/theme/koi.php";
}
if ($system['is_lz_game'] == 1) {
    require APP_PATH . "web/controller/theme/lz_game.php";
}
if ($system['lottery'] == 1) {
    require APP_PATH . "web/controller/theme/lottery.php";
}
if ($system['is_mystery_boxes'] > 0 || $system['is_mystery_boxes_feichi'] > 0 || $system['softmenu'] == 1 || $system['softmenu'] == 3) {
    require APP_PATH . "web/controller/Theme_mystery_boxes.php";
}
if ($system['is_lz_chest'] == 1) {
    require APP_PATH . "web/controller/theme/lingze.php";
}
if ($system['is_shop_plan'] == 1) {
    require APP_PATH . "web/controller/theme/shopplan.php";
}
//红包引入配置
if ($system['daiyan'] == 1) {
    require APP_PATH . "web/controller/theme/daiyan.php";
}
//红包雨引入配置
if ($system['redrain'] > 0) {
    require APP_PATH . "web/controller/theme/redrain.php";
}
//微客引入配置
if ($system['ruanjian'] == 10) {
    require APP_PATH . "web/controller/theme/weike.php";
}
//摇一摇多礼品
if ($system['fenxiao071'] == 1) {
    $yx_gift_name = $_P['yx_gift_name'];
    $yx_gift_detail = $_P['yx_gift_detail'];
    $yx_gift_price = $_P['yx_gift_price'];
    $yx_gift_minodds = $_P['yx_gift_minodds'];
    $yx_gift_maxodds = $_P['yx_gift_maxodds'];
    $yx_gift_stock = $_P['yx_gift_stock'];
    $yx_gift_status = $_P['yx_gift_status'];
    $yx_gift_img = $_P['yx_gift_img'];//单图详情
    $yx_gift_info = $_P['yx_gift_info0'];//多图详情
    $all_gift_arr = [];
    foreach ($yx_gift_name as $key => $val) {
        $temp_yx_gift_arr = [
            'id' => $key + 1,
            'yx_gift_name' => $val,
            'yx_gift_detail' => $yx_gift_detail[$key],
            'yx_gift_price' => $yx_gift_price[$key] ? $yx_gift_price[$key] : 0,
            'yx_gift_minodds' => $yx_gift_minodds[$key] ? intval($yx_gift_minodds[$key]) : 0,
            'yx_gift_maxodds' => $yx_gift_maxodds[$key] ? intval($yx_gift_maxodds[$key]) : 0,
            'yx_gift_stock' => intval($yx_gift_stock[$key]),
            'yx_gift_status' => $yx_gift_status[$key],
            'yx_gift_img' => $yx_gift_img[$key] ? tomedia($yx_gift_img[$key]) : ''
        ];
        $temp_key = 'yx_gift_info' . $key;
        if (isset($_P[$temp_key]) && $_P[$temp_key]) {
            if (is_array($_P[$temp_key])) {
                $temp_gift_info_arr = [];
                foreach ($_P[$temp_key] as $kk => $vv) {
                    $temp_url = $vv ? tomedia($vv) : '';
                    array_push($temp_gift_info_arr, $temp_url);
                }
                $temp_yx_gift_arr['yx_gift_info'] = $temp_gift_info_arr;
            } else {
                $temp_yx_gift_arr['yx_gift_info'] = [];
            }
        } else {
            $temp_yx_gift_arr['yx_gift_info'] = [];
        }
        if ($config['yx_gifts'] && is_array($config['yx_gifts'])) {
            array_push($all_gift_arr, $temp_yx_gift_arr);
        } else {
            if ($yx_gift_name[$key]) {
                array_push($all_gift_arr, $temp_yx_gift_arr);
            }
        }
    }


    //活动规则
    $yx_gt_rules = [];
    $yx_gt_rule_ids = $_P['yx_gt_rule_id'];
    $yx_gt_rule_sort = $_P['yx_gt_rule_sort'];
    $yx_gt_rule_content = $_P['yx_gt_rule_content'];
    if (is_array($yx_gt_rule_ids)) {
        foreach ($yx_gt_rule_ids as $k => $v) {
            $dd = ['rule_content' => $yx_gt_rule_content[$k], 'rule_sort' => $yx_gt_rule_sort[$k]];
            $yx_gt_rules[] = $dd;
        }
    }
    //右下角进入我的礼品图标
    $yx_gift_icon = $_P['yx_gift_icon'] ? tomedia($_P['yx_gift_icon']) : '';

    $tempconfig = [
        'yx_gifts' => $all_gift_arr,
        'yx_gt_rules' => $yx_gt_rules,//活动规则
        'yx_gift_icon' => $yx_gift_icon,//我的礼品图标
    ];
    $config = array_merge($config, $tempconfig);
}
//客资收集表单引入配置
if (Common::get_open_other_form($system, $config)) {
    require_once APP_PATH . "web/controller/Theme_other_form.php";
}
//优惠券
if ($system['yx_coupon'] == 1) {
    require_once APP_PATH . "web/controller/theme/coupon.php";
}
//多选分类
if ($_P['shopstype'] == 8 || $_P['presale_shopstype'] == 8) {
    if ($config['shopstype'] == 8 || $_P['presale_shopstype'] == 8) {
        $tmp_max_pid = 1;
        $tmp_max_cid = 1;
        $tmp_cate_info = [
            'dx_cate' => [],
            'dx_max_pid' => $tmp_max_pid,
            'dx_max_cid' => $tmp_max_cid,
            'dx_product' => [],
        ];
        $dx_cate = $_P['dx_cate'];
        $dx_product = $_P['dx_product'];

        $tmp_cate_only_data = [];
        $tmp_cate = [];
        if (!empty($dx_cate)){
            foreach ($dx_cate as $v) {
                $ctmp = [
                    'id' => $v['id'],
                    'name' => $v['name'],
                    'min' => $v['min'],
                    'max' => $v['max'],
                    'status' => $v['status'],
                    'list' => [],
                ];
                if ($v['name'] && !empty($v['name'])) {
                    array_push($tmp_cate_only_data, $ctmp);
                    array_push($tmp_cate, $ctmp);
                    if ((int)$v['id'] > $tmp_max_cid) {
                        $tmp_max_cid = (int)$v['id'];
                    }
                }
            }
        }
        $tmp_product = [];
        foreach ($tmp_cate as $key => $val) {
            if ($dx_product) {
                foreach ($dx_product as $vv) {
                    if ($vv['c_id'] == $val['id']) {
                        $tmp_checked = $vv['is_default'] == 1 ? true : false;
                        $tmp_status = $vv['status'];
                        if ($val['status'] == 0) {
                            $tmp_status = 0;
                        }
                        if ($vv['name']==''){
                            $sql = "select count(*) as cnt from ".tablename(t_record)." where themeid='$themeid'";
                            $has_flag = db_find($sql)['cnt'];
                            if ($has_flag){
                                foreach ($config_old['dx_product'] as $k1 => $v2){
                                    if ($vv['id']==$v2['id']){
                                        $vv['name'] = $v2['name'];
                                        $tmp_status = 0;
                                    }
                                }
                            }
                        }
                        $ptmp = [
                            'id' => $vv['id'],
                            'name' => $vv['name'],
                            'c_id' => $vv['c_id'],
                            'price' => isset($vv['price']) ? (float)$vv['price'] : 0,
                            'qty' => isset($vv['qty']) ? (int)$vv['qty'] : 9999,
                            'sj_id' => $vv['sj_id'] ? $vv['sj_id'] : 0,
                            'is_default' => $vv['is_default'],
                            'checked' => $tmp_checked,
                            'status' => $tmp_status
                        ];
                        //多选分类 分类下产品项  判断name为空 提出不进行保存 解决使用过程中显示为空
                        if ($vv['name']){
                            array_push($tmp_product,$ptmp);
                            $tmp_cate[$key]['list'][] = $ptmp;
                            if ((int)$vv['id']>$tmp_max_pid){
                                $tmp_max_pid = (int)$vv['id'];
                            }
                        }
                    }
                }
            }

        }
        if ($config['shopstype'] == 8){
            $tmp_cate_info['options'] = $tmp_cate;
        }
        $tmp_cate_info['dx_cate'] = $tmp_cate;
        $tmp_cate_info['dx_product'] = $tmp_product;
        $tmp_cate_info['dx_cate_only_data'] = $tmp_cate_only_data;
        $tmp_cate_info['dx_max_pid'] = $tmp_max_pid;
        $tmp_cate_info['dx_max_cid'] = $tmp_max_cid;
        $config = array_merge($config, $tmp_cate_info);
        if ($_P['presale_shopstype']==8 && $tmp_cate){
            $config['presale']['gudingoptions'] = [];
            $config['presale']['options'] = $tmp_cate;
        }
    }

}
//判断启用-多选分类库存
if ($_P['form_five_qty'] == 1) {
    $tmp_config = [];
    $tmp_config['form_five_qty'] = (int)$_P['form_five_qty'];
    $config = array_merge($config, $tmp_config);
    if ($config_old['form_five_qty'] != 1){
        //创建用户记录表
        $sql = "
                CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_form_five_user_flag` (
                `id` int(11) not null AUTO_INCREMENT,
                `order_id` int(11) not null default 0 comment '订单id',
                `form_item_id` int(11) not null default 0 comment '文本5_item_id',
                `form_item_name` varchar(64) not null default 0 comment '文本5_item_name',
                `user_id` int(11) not null default 0 comment '用户id',
                `themeid` varchar(11) not null default '' comment '活动id',
                PRIMARY KEY (`id`)
                )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='文本5用户选择记录表';
            ";
        db_run($sql);
        yxlog('创建文本5多选分类用户选择记录表');
    }
}
//判断启用-异业多选分类绑定商家功能
if ($_P['form_five_qty_bind_shangjia'] == 1){
    $tmp_config = [];
    $tmp_config['form_five_qty_bind_shangjia'] = (int)$_P['form_five_qty_bind_shangjia'];
    $config = array_merge($config, $tmp_config);
}

//车牌号  针对于2、3、5  有且仅有一个
if (($_P['address'] && $_P['addresstype'] == 9) || ($_P['formtwo'] && $_P['formtwotype'] == 9) || ($_P['shops'] && $_P['shopstype'] == 9)) {
    //判断仅有一个
    $car_num_type_total = 0;
    if ($_P['addresstype'] == 9) {
        $car_num_type_total += 1;
    }
    if ($_P['formtwotype'] == 9) {
        $car_num_type_total += 1;
    }
    if ($_P['shopstype'] == 9) {
        $car_num_type_total += 1;
    }
    if ($car_num_type_total > 1) {
        return_msg('车牌号类型，文本2、3、5仅能启用一项');
        die();
    }
}
if ($_P['open_car_num_type']) {
    $tmp_config = [];
    $tmp_config['open_car_num_type'] = (int)$_P['open_car_num_type'];
    $config = array_merge($config, $tmp_config);
}

//仅针对小程序手机号输入模式  表单1-3
if ($system['softmode']==1){
    $tmp_config = [];
    if (isset($_P['mobile_input_type'])){
        $tmp_config['mobile_input_type'] = (int)$_P['mobile_input_type'];
    }
    if (isset($_P['mobile_input_type2'])){
        $tmp_config['form2'] = $config['form2'];
        $tmp_config['form2']['mobile_input_type'] = (int)$_P['mobile_input_type2'];
    }
    if (isset($_P['mobile_input_type3'])){
        $tmp_config['form3'] = $config['form3'];
        $tmp_config['form3']['mobile_input_type'] = (int)$_P['mobile_input_type3'];
    }
    $config = array_merge($config, $tmp_config);
}

//日期类型 文本1-5 判断是否设置日期段
$tmp_date_config = [];
if ($_P['datetimetype'] == 10 && $_P['datetime']) {
    //文本1
    $start1 = strtotime($_P['datetimelimit']['start']);
    $end1 = strtotime($_P['datetimelimit']['end']);

    if ($start1 > $end1) {
        return_msg('文本1 日期起止时间不合法' . $start1);
        die();
    }
    $tmp_date_config['datetimelimit_start'] = $start1 == $end1 ? '' : $_P['datetimelimit']['start'];
    $tmp_date_config['datetimelimit_end'] = $start1 == $end1 ? '' : $_P['datetimelimit']['end'];
}

if ($_P['formtwotype'] == 10 && $_P['formtwo']) {
    //文本3
    $start1 = strtotime($_P['formtwolimit']['start']);
    $end1 = strtotime($_P['formtwolimit']['end']);
    if ($start1 > $end1) {
        return_msg('文本3 日期起止时间不合法');
        die();
    }
    $tmp_date_config['formtwolimit_start'] = $start1 == $end1 ? '' : $_P['formtwolimit']['start'];
    $tmp_date_config['formtwolimit_end'] = $start1 == $end1 ? '' : $_P['formtwolimit']['end'];
}
//显示分享人
if ($_P['isdanmu'] == 3) {
    $config['isdanmu'] = 1;
    $tmp_date_config['show_fromuid_info'] = 1;
} else {
    $tmp_date_config['show_fromuid_info'] = 0;
}

//员工注册
$tmp_date_config['isnewreg'] = intval($_P['isnewreg']) == 1 ? 1 : 0;
$tmp_date_config['newregbg'] = $_P['newregbg'] ? tomedia($_P['newregbg']) : '';     //注册成功背景
$tmp_date_config['newregbtntxt'] = $_P['newregbtntxt'] ? trim($_P['newregbtntxt']) : '进入活动开始邀约';
$tmp_date_config['newregbtncolor'] = $_P['newregbtncolor'] ? trim($_P['newregbtncolor']) : '#ec1f1a';
$tmp_date_config['newregbtntop'] = intval($_P['newregbtntop']) != 0 ? intval($_P['newregbtntop']) : 85;

//底部客服专属二维码相关设置
$tmp_date_config['private_kfewm_bg_img'] = $_P['private_kfewm_bg_img']?tomedia($_P['private_kfewm_bg_img']):'';
$tmp_date_config['private_kfewm_bg_color'] = $_P['private_kfewm_bg_color']?$_P['private_kfewm_bg_color']:'#ffffff';
$tmp_date_config['private_kfewm_title'] = $_P['private_kfewm_title']?$_P['private_kfewm_title']:'请长按图片识别，添加专属客服二维码';
$tmp_date_config['private_kfewm_fontsize'] = $_P['private_kfewm_fontsize']?$_P['private_kfewm_fontsize']:18;
$tmp_date_config['private_kfewm_line_height'] = $_P['private_kfewm_line_height']?$_P['private_kfewm_line_height']:30;
$tmp_date_config['private_kfewm_title_color'] = $_P['private_kfewm_title_color']?$_P['private_kfewm_title_color']:'#000000';
$tmp_date_config['private_kfewm_top'] = $_P['private_kfewm_top']?$_P['private_kfewm_top']:'50';
$tmp_date_config['private_kfewm_left'] = $_P['private_kfewm_left']?$_P['private_kfewm_left']:'50';
$tmp_date_config['private_kfewm'] = intval($_P['private_kfewm']);
//阶梯团礼品模式
$tmp_date_config['step_tuan_gift_mode'] = intval($_P['step_tuan_gift_mode']);

if (!empty($_P['only_stu_data'])){
    //创建表
    $sql = " CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_stu_data` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `phone` char(32) NOT NULL COMMENT '加密手机号码',
                  `themeid` int(11) NOT NULL,
                  `addtime` int(11) NOT NULL,
                   PRIMARY KEY (`id`),
                   UNIQUE KEY `uni_phone` (`phone`,`themeid`) USING BTREE
                )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='新生老生手机号记录表';
            ";
    db_run($sql);
//    yxlog('创建新生老生手机号记录表');
}
//限制采集手机号开启 暂时性屏蔽 后续再考虑
//if(intval($_P['getmobile'])>0){
//    $accesstoken = getAccessToken($system,true);
//    $GetUsageDetail = WxCharge::Limit($accesstoken);
//    if($GetUsageDetail['code'] != 200){
//        return_msg($GetUsageDetail['msg']);
//        die();
//    }
//}
//隐私模式
if (isset($_P['private_deal_mode'])) {
    $tmp_config['private_deal_mode'] = $_P['private_deal_mode'];
    $config = array_merge($config, $tmp_config);
}
$config = array_merge($config, $tmp_date_config);
